import Vue from 'vue'
import Vuex from 'vuex'
import Storage from 'vue-ls'
import Stomp from 'stompjs';
Vue.use(Vuex)
let messageList = [],
	wx_messageList = [],
	sign_messageList = [],
	feedback_messageList = [],
	audio_messageList = [],
	que_messageList = [];
const rabbitMQStore = {
	state: {
		conncted: '',
		client: null,
		currentSubscribe: null,
		options: {
			vhost: '/', // rabbitmq的vhost
			incoming: 8000, // 心跳包时间，（须大于0，且小于10000，因为服务器可能默认10秒没心跳就会断开）
			outgoing: 8000, // 心跳包时间，（须大于0，且小于10000，因为服务器可能默认10秒没心跳就会断开）
			account: process.env.VUE_APP_RABBITMQ_API == 'ws://192.168.30.193:15674/ws/' ? 'admin' :
			'guest', // rabbitmq的账户
			pwd: process.env.VUE_APP_RABBITMQ_API == 'ws://192.168.30.193:15674/ws/' ? 'admin' :
			'guest', // rabbitmq的密码
			server: process.env.VUE_APP_RABBITMQ_API // ws://rabbitmq的ip:rabbitmq的web-stomp的端口/ws

		}
	},
	mutations: {
		RABBITMQ_INIT(state) {
			let that = this;

			if (state.conncted) {
				return
			}
			const mqUrl = state.options.server; // 连接地址
			const ws = new WebSocket(mqUrl); // 创建
			ws.onclose = close => {
				console.log('webSocket关闭', close); // 关闭回调
			};
			ws.onerror = error => {
				console.log('webSocket异常', error); // 异常回调
			};
			ws.onopen = success => {
				console.log('webSocket连接成功', success); // 成功回调
			};
			state.client = Stomp.over(ws);
			state.client.heartbeat.incoming = state.options.incoming;
			state.client.heartbeat.outgoing = state.options.outgoing;

			//开始连接
			state.client.connect(
				state.options.account, // 用户名
				state.options.pwd, // 密码
				() => {
					console.log('stomp 连接成功！');
					state.conncted = true;
					//that.commit('doSubscribe', '/queue/queue_answ');
					//onSuccessConnectRabbitmq
					//把需要再次上次的消息推送成功
					if (messageList && messageList.length > 0) {
						messageList.forEach(item => {
							that.commit('RABBITMQ_SEND', item);
						})
					}
					if (sign_messageList && sign_messageList.length > 0) {
						sign_messageList.forEach(item => {
							that.commit('sign_RABBITMQ_SEND', item);
						})
					}
					if (wx_messageList && wx_messageList.length > 0) {
						wx_messageList.forEach(item => {
							that.commit('wx_RABBITMQ_SEND', item);
						})
					}
					if (feedback_messageList && feedback_messageList.length > 0) {
						feedback_messageList.forEach(item => {
							that.commit('feedback_RABBITMQ_SEND', item);
						})
					}

					if (audio_messageList && audio_messageList.length > 0) {
						audio_messageList(item => {
							that.commit('audio_RABBITMQ_SEND', item);
						})
					}
					if (que_messageList && que_messageList.length > 0) {
						que_messageListforEach(item => {
							that.commit('que_RABBITMQ_SEND', item);
						})
					}
					sign_messageList = [];
					wx_messageList = [];
					messageList = [];
					feedback_messageList = [];
				}, // 连接成功时回调
				(errorMsg) => {
					//onErrorConnectRabbitmq
					state.conncted = false;
					console.error(`stomp 断开连接，正在准备重新连接...`, errorMsg);
					/* 判断是否需要重连 */
					let beginClassData = Vue.ls.get('beginClass');
					if (beginClassData && beginClassData.csId) {
						that.commit('RABBITMQ_INIT');
					}
				}, // 失败时回调
				state.options.vhost
			);

		},
		RABBITMQ_SEND(state, message) {
			if (state.conncted) {
				state.client.send('/exchange/answ.exchange/queue_answ', {
					'content-type': 'text/plain'
				}, message);
			} else {
				messageList.push(message)
			}
		},
		//向微信发送答题进度
		wx_RABBITMQ_SEND(state, message) {
			if (state.conncted) {
				state.client.send('/exchange/answ.exchange/queue_ansprog', {
					'content-type': 'text/plain'
				}, message);
			} else {
				messageList.push(message)
			}
		},
		/* 推送签到信息 */
		sign_RABBITMQ_SEND(state, message) {
			if (state.conncted) {
				state.client.send('/exchange/answ.exchange/queue_signin', {
					'content-type': 'text/plain'
				}, message);
			} else {
				sign_messageList.push(message)
			}
		},
		/* 推送签到信息 */
		feedback_RABBITMQ_SEND(state, message) {
			if (state.conncted) {
				state.client.send('/exchange/answ.exchange/queue_feedback', {
					'content-type': 'text/plain'
				}, message);
			} else {
				feedback_messageList.push(message)
			}
		},
		//语音识别推送
		audio_RABBITMQ_SEND(state, message) {
			if (state.conncted) {
				state.client.send('/exchange/answ.exchange/winqueue_voice', {
					'content-type': 'text/plain'
				}, message);
			} else {
				audio_messageList.push(message)
			}
		},
		//语音问答题推送
		que_RABBITMQ_SEND(state, message) {
			if (state.conncted) {
				state.client.send('/exchange/answ.exchange/winrkqu_voice', {
					'content-type': 'text/plain'
				}, message);
			} else {
				que_messageList.push(message)
			}
		},
		//阅读题推送
		que_JOBVOICE_SEND(state, message) {
			if (state.conncted) {
				state.client.send('/exchange/answ.exchange/job_voice', {
					'content-type': 'text/plain'
				}, message);
			} else {
				que_messageList.push(message)
			}
		},
		//发送语音测评内容
		que_CPTEXT_SEND(state, message) {
			if (state.conncted) {
				state.client.send('/exchange/answ.exchange/wincosyp_voice', {
					'content-type': 'text/plain'
				}, message);
			} else {
				// messageList.push(message)
			}
		},
		//语音测评 语音推送
		que_CPVOICE_SEND(state, message) {
			if (state.conncted) {
				state.client.send('/exchange/answ.exchange/wincpqueue_voice', {
					'content-type': 'text/plain'
				}, message);
			} else {
				//que_messageList.push(message)
			}
		},

		onSuccessConnectRabbitmq(state) {

			// 直接在连接成功的时候就订阅监听user.audit.queue队列 user.audit.queue是rabbitmq里创建的queue名称
			// this.doSubscribe('/queue/test');


		},
		onErrorConnectRabbitmq(errorMsg) {

		},
		doSubscribe(state, queueName) {
			state.currentSubscribe = state.client.subscribe(queueName, function(messages) {
				console.log('receive:', messages);
				console.log('message body', messages.body); // 消息内容主体 json需要自己转
			});
		},
		doUnSubscribe(state) {
			state.currentSubscribe.unsubscribe();
		},
		createConnection(state) {
			this.commit('RABBITMQ_INIT');

		},
		destroyConnection(state) {
			state.client.disconnect(() => {
				console.log('已关闭rabbitmq连接');
				state.conncted = false;
			});
		}

	},
	actions: {

	}
}
export default rabbitMQStore